<?php

$id_cladire = "6";
$clasa = "3";
$k = 0;

//Creare vector ce contine tipul de unitati ce se pot construi la momentul actual.
$query = mysql_query("SELECT * FROM c_unitati WHERE clasa = '".$clasa."'");
while($rez = mysql_fetch_array($query)) {
 list($cladire_1, $nivel_1) = explode("@", $rez["cerinte"]);
 $nivel_2 = valoare_mysql("SELECT nivel FROM cladiri WHERE planeta = '".$planeta."' AND nume = '".$cladire_1."'");
if ($nivel_1 <= $nivel_2) {
$vector[$k] = $rez["id"];
$k++;
 }
}

//Verificare daca exista parametru transmis, iar daca da, parcurgere string si proceare valori.
if ($string != "")
{
$v = explode("@", $string);
if ($v[0] == $k) {
for($i=1; $i<=$v[0]; $i++) {
$x = valoare_mysql("SELECT id FROM actiuni_locale WHERE planeta = '".$planeta."' AND tip = 'construire_unitati' AND santier = '".$clasa ."' AND obiect = '".$vector[$i-1]."'");

if ($x == "")
{
 $id = md5(time().$i);
 $durata = valoare_mysql("SELECT durata FROM c_unitati WHERE clasa = '".$clasa."' AND id = '".$vector[$i-1]."'");
  $query_cost = mysql_fetch_row(mysql_query("SELECT cost_minerale, cost_energie, cost_populatie FROM c_unitati WHERE clasa = '".$clasa."' AND id = '".$vector[$i-1]."'"));
 $y = $query_cost[2]*$v[$i];
 mysql_query("UPDATE users SET populatie = populatie + '".$y."' WHERE user = '".$user."'");
 mysql_query("UPDATE planete SET minerale = minerale - '".$query_cost[0]."', energie = energie - '".$query_cost[1]."' WHERE id = '".$planeta."'");
 mysql_query("INSERT INTO actiuni_locale (id, tip, planeta, durata, incepere, santier, obiect, valoare) VALUES ('".$id."', 'construire_unitati', '".$planeta."', '".$durata."', NOW( ), '".$clasa."','".$vector[$i-1]."', '".$v[$i]."')");
} else {
 $x = valoare_mysql("SELECT valoare FROM actiuni_locale WHERE planeta = '".$planeta."' AND tip = 'construire_unitati' AND santier = '".$clasa ."' AND obiect = '".$vector[$i-1]."'");
 $nr_unitati = $x + $v[$i];
 $query_cost = mysql_fetch_row(mysql_query("SELECT cost_minerale, cost_energie, cost_populatie FROM c_unitati WHERE clasa = '".$clasa."' AND id = '".$vector[$i-1]."'"));

 $y = $query_cost[2]*$nr_unitati; 
 $z = $query_cost[0]*$nr_unitati;
 $b = $query_cost[1]*$nr_unitati;
 mysql_query("UPDATE users SET populatie = populatie + '".$y."' WHERE user = '".$user."'");
 mysql_query("UPDATE planete SET minerale = minerale - '".$z."', energie = energie - '".$b."' WHERE id = '".$planeta."'");
 mysql_query("UPDATE actiuni_locale SET valoare = '".$nr_unitati."' WHERE planeta = '".$planeta."' AND tip = 'construire_unitati' AND santier = '".$clasa ."' AND obiect = '".$vector[$i-1]."'");
}
   }
 }
}

//Afisare tipuri de unitati ce se pot construi la momentul actual.
$query = mysql_query("SELECT * FROM c_unitati WHERE clasa = '".$clasa."'");
 echo '<form id="optiuni_1" name="optiuni_1" method="GET" action="#" onclick="javascript:return false;">
<table width="353" border="0" class="font_2">';
while($rez = mysql_fetch_array($query)) {
 list($cladire_1, $nivel_1) = explode("@", $rez["cerinte"]);
 $nivel_2 = valoare_mysql("SELECT nivel FROM cladiri WHERE planeta = '".$planeta."' AND nume = '".$cladire_1."'");
 $momentan = valoare_mysql("SELECT nr FROM unitati WHERE planeta = '".$planeta."' AND tip = '".$rez["id"]."' AND `locatie` = 'disponibili'");
 if ($momentan == "") 
  $momentan = 0;
  $query_resurse = mysql_fetch_row(mysql_query("SELECT minerale, energie FROM planete WHERE id = '".$planeta."'"));
  $query_cost = mysql_fetch_row(mysql_query("SELECT cost_minerale, cost_energie, cost_populatie FROM c_unitati WHERE id = '".$rez["id"]."' AND clasa = '".$clasa."'"));
  $nivel_depozit_aprovizionare = valoare_mysql("SELECT nivel FROM `cladiri` WHERE nume = '3' AND planeta = '$planeta'", 0); //Nivel depozit aprovizionare
  //Calculare maximul de muncitori ce se pot construi din resursele actuale
  if ($nivel_depozit_aprovizionare > 0)
   $max_populatie = round(formula_incrementare(20, $nivel_depozit_aprovizionare, '2.5', ''))+20;
  else
   $max_populatie = 20;
  $populatie = returnare_populatie($planeta) + returnare_populatie_extra($planeta);
  $total = $max_populatie - $populatie;
  $max_1 = floor($query_resurse[0]/$query_cost[0]);
  $max_2 = floor($query_resurse[1]/$query_cost[1]);
  $max_3 = floor($total/$query_cost[2]);

  $max = min($max_1, $max_2, $max_3);
if ($nivel_1 <= $nivel_2) {
echo '<tr><td width="154">';
echo $rez["nume"].'(momentan '.$momentan.')</td>'; 
echo '<td width="24"><input type="text" name="optiuni_value" size="3" maxlength="4" /><input type="hidden" name="optiuni_max" size="3" maxlength="4" value="'.$max.'" /></td>
  <td width="46">unitati</td>
  <td width="94"><input type="submit" name="construieste" value="Construieste" onMouseUp="javascript:reimprospatare(\'unitati\');optiuni_structura(\''.$id_cladire.'\', \'string\');" /></td>
    </tr>';
	}
  }

echo '</table></form>';

?>